package com.project.datacenter.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.project.datacenter.dto.BusLineStationDto;
import com.project.datacenter.dto.ParamDto;
import com.project.datacenter.entity.BusLine;
import com.project.datacenter.entity.BusLineStation;
import com.project.datacenter.entity.TobjBusLineStation;
import com.project.datacenter.mapper.BusLineMapper;
import com.project.datacenter.mapper.BusLineStationMapper;
import com.project.datacenter.mapper.TobjBusLineStationMapper;
import com.project.datacenter.service.BusLineStationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BusLineStationServiceImpl implements BusLineStationService {

    @Resource
    private BusLineStationMapper busLineStationMapper;
    @Resource
    private TobjBusLineStationMapper tobjBusLineStationMapper;
    @Resource
    private BusLineMapper busLineMapper;

    @Override
    @DS("datacenter")
    public List<TobjBusLineStation> getList() {
        return tobjBusLineStationMapper.getList();
    }

    @Override
    @DS("master")
    public List<TobjBusLineStation> getListByDate(ParamDto paramDto) {
        return tobjBusLineStationMapper.selectByDate(paramDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void saveList(List<TobjBusLineStation> list) {
        for (TobjBusLineStation entity : list) {
            tobjBusLineStationMapper.insertSelective(entity);
        }
    }

    @Override
    @DS("datacenter")
    public List<TobjBusLineStation> getFirst() {
        return tobjBusLineStationMapper.getFirst();
    }

    @Override
    @DS("datacenter")
    public List<TobjBusLineStation> getMax() {
        return tobjBusLineStationMapper.getMax();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void deleteByDate(ParamDto paramDto) {
        tobjBusLineStationMapper.deleteByDate(paramDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void deleteExpireDate(ParamDto paramDto) {
        tobjBusLineStationMapper.deleteExpireDate(paramDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void update(List<TobjBusLineStation> list) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Example example = new Example(BusLine.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("state", 0);
        List<BusLine> busLineList = busLineMapper.selectByExample(example);
        for (TobjBusLineStation tobjBusLineStation : list) {
            BusLineStation busLineStation = new BusLineStation();
            busLineStation.setId(tobjBusLineStation.getBusstop_id());
            busLineStation.setXlid(tobjBusLineStation.getBusline_id());
            if ("2".equals(tobjBusLineStation.getDirectiontype())) {
                busLineStation.setZxlx("0");
            } else {
                busLineStation.setZxlx(tobjBusLineStation.getDirectiontype());
            }
            if (tobjBusLineStation.getStationlevel() != null) {
                busLineStation.setZj(String.valueOf(tobjBusLineStation.getStationlevel()));
            }
            busLineStation.setZdid(tobjBusLineStation.getStation_id());
            busLineStation.setZdfmc(tobjBusLineStation.getStationname());
            busLineStation.setSbsj(tobjBusLineStation.getStarttime());
            busLineStation.setMbsj(tobjBusLineStation.getEndtime());

            BusLineStationDto busLineStationDto = new BusLineStationDto();
            busLineStationDto.setXlid(tobjBusLineStation.getBusline_id());
            if (busLineList.size() > 0) {
                List<BusLine> busLineList0 = busLineList.stream().filter(item -> item.getYgxlid().equals(tobjBusLineStation.getBusline_id())).collect(Collectors.toList());
                if (busLineList0.size() > 0) {
                    if ("3".equals(busLineList0.get(0).getYysj())) {
                        if (!StringUtils.isEmpty(tobjBusLineStation.getPeakendtime())) {
                            busLineStation.setMbsj(tobjBusLineStation.getPeakendtime());
                        }
                    }
                    if (!StringUtils.isEmpty(tobjBusLineStation.getEndtime()) && !StringUtils.isEmpty(tobjBusLineStation.getPeakendtime())) {
                        String endTime = tobjBusLineStation.getEndtime();
                        if (tobjBusLineStation.getEndtime().length() < 5) {
                            endTime = "0" + endTime;
                        }
                        String peakendTime = tobjBusLineStation.getPeakendtime();
                        if (tobjBusLineStation.getPeakendtime().length() < 5) {
                            peakendTime = "0" + peakendTime;
                        }
                        endTime = endTime + ":00";
                        peakendTime = peakendTime + ":00";
                        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
                        LocalTime endTimeT = LocalTime.parse(endTime, formatter);
                        LocalTime peakendTimeT = LocalTime.parse(peakendTime, formatter);
                        if (peakendTimeT.isBefore(endTimeT)) {
                            busLineStation.setMbsj(tobjBusLineStation.getEndtime());
                        }
                    }
                }
            }
            if (tobjBusLineStation.getStationdistance() != null) {
                busLineStation.setZdjl(tobjBusLineStation.getStationdistance());
            }
            busLineStation.setSzfs(tobjBusLineStation.getStationmode());
            busLineStation.setZtlx(tobjBusLineStation.getStationboothtype());
            busLineStation.setDldm(tobjBusLineStation.getRoad_id());
            busLineStation.setXzqdm(tobjBusLineStation.getDistrictcode());
            busLineStation.setHxwz(tobjBusLineStation.getLooplocation());
            if (tobjBusLineStation.getAddtime() != null) {
                busLineStation.setAddtime(sdf.format(tobjBusLineStation.getAddtime()));
            }
            busLineStation.setSzxz("0");
        }
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void deleteAll() {
        busLineStationMapper.deleteAll();
    }

    @Override
    @DS("master")
    public List<BusLineStationDto> getRepeatAll() {
        return busLineStationMapper.getRepeatAll();
    }

    @Override
    @DS("master")
    public List<BusLineStation> getByRepeat(BusLineStationDto dto) {
        return busLineStationMapper.getByRepeat(dto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void deleteById(BusLineStation busLineStation) {
        busLineStationMapper.deleteById(busLineStation);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void updateSzmbById(BusLineStation busLineStation) {
        busLineStationMapper.updateSzmbById(busLineStation);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void updateMzsbById(BusLineStation busLineStation) {
        busLineStationMapper.updateMzsbById(busLineStation);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void updateYgmc() {
        List<BusLineStation> SzmcList =  busLineStationMapper.getSzmc();
        for(BusLineStation busLineStation : SzmcList){
            busLineMapper.updateYgSzmc(busLineStation);
        }
        List<BusLineStation> MzmcList =  busLineStationMapper.getMzmc();
        for(BusLineStation busLineStation : MzmcList){
            busLineMapper.updateYgMzmc(busLineStation);
        }
    }
}
